package com.inossem.warehousestorage_inossem.adapter.allocation;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.AllocationInMatsActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.AllocationInPaperActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInPapersBean;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/4/3 15:14
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInPapersAdapter extends BaseAdapter {

    private Context mContext;
    private List<AllocationInPapersBean.BodyBean> list;
    private String mCondition;
    private AllocationInPaperActivity.SearchListener listener;

    public AllocationInPapersAdapter(Context context, List<AllocationInPapersBean.BodyBean> list, String mCondition, AllocationInPaperActivity.SearchListener listener) {
        this.mContext = context;
        this.list = list;
        this.mCondition = mCondition;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parentp) {
        if (position == 0) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_out_storage_picking_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setText(mCondition);
            condition.setHint("输入调拨出库单号");
            search.setOnClickListener(listener.getNoDoubleClickListener(condition));
        } else {
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.cy_view_in_storage_allocation_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            AllocationInPapersBean.BodyBean b = list.get(position - 1);
            initData(mContext, viewHolder, convertView, b);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView codeTv, placeTv, personTv, timeTv;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.codeTv = (TextView) view.findViewById(R.id.codeTv);
        viewHolder.placeTv = (TextView) view.findViewById(R.id.placeTv);
        viewHolder.personTv = (TextView) view.findViewById(R.id.personTv);
        viewHolder.timeTv = (TextView) view.findViewById(R.id.timeTv);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, AllocationInPapersBean.BodyBean b) {
        viewHolder.codeTv.setText(b.getAllocate_code());
        viewHolder.placeTv.setText(b.getLocation_output_name());
        viewHolder.personTv.setText(b.getApplicant());
        viewHolder.timeTv.setText(b.getDemand_date());

        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, AllocationInMatsActivity.class);
                intent.putExtra(AllocationInStorageExtra.ALLOCATE_CODE, b.getAllocate_code());
                intent.putExtra(AllocationInStorageExtra.ALLOCATE_ID, b.getAllocate_id());
                context.startActivity(intent);
            }
        });
    }
}
